package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyArrearPeopleManageDTO;
import com.itlong.cloud.POJO.PO.ArrearageRoomManageInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyArrearPeopleManageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyArrearPeopleManageDao;
import com.itlong.cloud.property.service.IPropertyArrearPeopleManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      欠费人员管理服务层接口实现类
 * </desc>
 *
 * @createDate 2019/05/22
 */
@Service
public class PropertyArrearPeopleManageServiceImpl implements IPropertyArrearPeopleManageService {

    @Autowired
    private IPropertyArrearPeopleManageDao iPropertyArrearPeopleManageDao;

    /**
     * <desc>
     *      分页获取欠费人员管理信息
     * </desc>
     *
     * @param propertyArrearPeopleManageDTO 欠费人员管理DTO
     * @return 欠费人员管理分页信息
     * @author wangzhi
     * @createDate 2019/05/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyArrearPeopleManageVO> getPage(PropertyArrearPeopleManageDTO propertyArrearPeopleManageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyArrearPeopleManageDTO);
        Page<PropertyArrearPeopleManageVO> page = new Page<>();
        page.getPage().setPageSize(propertyArrearPeopleManageDTO.getPageSize());
        page.getPage().setCurrentPage(propertyArrearPeopleManageDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<PropertyArrearPeopleManageVO> propertyArrearPeopleManageVOS = iPropertyArrearPeopleManageDao.getPage(params);
        page.setRows(propertyArrearPeopleManageVOS,iPropertyArrearPeopleManageDao.getRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      欠费人员管理添加地址
     * </desc>
     *
     * @param propertyArrearPeopleManageDTO 欠费人员管理DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2019/05/22
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(PropertyArrearPeopleManageDTO propertyArrearPeopleManageDTO) throws Exception {
        int count = iPropertyArrearPeopleManageDao.getExistCount(propertyArrearPeopleManageDTO.getProjectId(), propertyArrearPeopleManageDTO.getRoomId());
        if (count != 0) {
            return 1;
        }
        Date nowTime = new Date();
        ArrearageRoomManageInfoPO arrearageRoomManageInfoPO = new ArrearageRoomManageInfoPO();
        arrearageRoomManageInfoPO.setRoomId(propertyArrearPeopleManageDTO.getRoomId());
        arrearageRoomManageInfoPO.setUnitId(propertyArrearPeopleManageDTO.getUnitId());
        arrearageRoomManageInfoPO.setBuildId(propertyArrearPeopleManageDTO.getBuildId());
        arrearageRoomManageInfoPO.setProjectId(propertyArrearPeopleManageDTO.getProjectId());
        arrearageRoomManageInfoPO.setCreateUserId(propertyArrearPeopleManageDTO.getLoginUserId());
        arrearageRoomManageInfoPO.setOperateUserId(propertyArrearPeopleManageDTO.getLoginUserId());
        arrearageRoomManageInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        arrearageRoomManageInfoPO.setCreateTime(nowTime);
        arrearageRoomManageInfoPO.setUpdateTime(nowTime);
        return iPropertyArrearPeopleManageDao.save(SqlUtil.durableData(arrearageRoomManageInfoPO,PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      获取导出的欠费人员管理信息
     * </desc>
     *
     * @param propertyArrearPeopleManageDTO 欠费人员管理DTO
     * @return 欠费人员管理列表信息
     * @author wangzhi
     * @createDate 2019/05/22
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyArrearPeopleManageVO> getExportData(PropertyArrearPeopleManageDTO propertyArrearPeopleManageDTO) throws Exception {
        if (StringUtils.isBlank(propertyArrearPeopleManageDTO.getRoomIds())) {
            Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyArrearPeopleManageDTO);
            return iPropertyArrearPeopleManageDao.getAllData(params);
        }
        List<String> roomIdList = StringHandlerUtil.splitStringList(propertyArrearPeopleManageDTO.getRoomIds());
        return iPropertyArrearPeopleManageDao.getDataByRoomIds(roomIdList, propertyArrearPeopleManageDTO.getProjectId());
    }

    /**
     * <desc>
     *      获取项目绑定的分润规则类型
     * </desc>
     *
     * @param projectId 项目id
     * @return 项目绑定的分润规则类型
     * @author wangzhi
     * @createDate 2019/06/12
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public String getProjectProfitType(String projectId) throws Exception {
        return iPropertyArrearPeopleManageDao.getProjectProfitType(projectId);
    }
}
